home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / Communications Toolbox / CTB Sample Code 1.0b16 / CTB Sources / Sources 2 / Terminal Tool for CTB / tscr.p < prev    next >
Encoding:
Text File  |  1989-06-27  |  9.4 KB  |  383 lines  |  [TEXT/MPS ]

  1. { *** =================================================== ***
  2.  
  3.     Purpose:        Terminal tool's main scripting code
  4.         module.  Basically used to convert private 
  5.         structure values into strings (e.g., TMGetConfig).
  6.  
  7.     Module :        tscr.p
  8.  
  9.     Authors:        Craig Hotchkiss, Alex Kazim, Byron Han, 
  10.                         Carol Lee
  11.                     Apple Computer, Inc.
  12.                     Networks & Communications
  13.                     20525 Mariani Drive
  14.                     Cupertino, CA 95014
  15.  
  16.     Version :        1.0d1
  17.  
  18.     Date :            9.may.89
  19.  
  20.     History :
  21.         9.may.89    Creation date
  22.  
  23.  
  24.     ©1989 Apple Computer, Inc.  All Rights Reserved.
  25.     This software is proprietary to Apple Computer, Inc.
  26.     It may not be copied, in whole or in part,
  27.     without the written consent of Apple Computer, Inc.
  28.  
  29.   *** =================================================== *** }
  30.     
  31.  
  32.  
  33.  
  34. Unit TERMINALtscr;
  35.  
  36.  
  37. INTERFACE
  38.  
  39.  
  40. USES     MemTypes,
  41.         QuickDraw,
  42.         OSIntf,
  43.         ToolIntf,
  44.         PasLibIntf,
  45.         Lists,
  46.         CRMSerialIntf,
  47.         CRMIntf,
  48.         CTBUtils,
  49.         CMIntf,
  50.         TMIntf,
  51.         FTIntf,
  52.         TerminalTool,
  53.         TermGlobalUnit;
  54.         
  55.         
  56.     
  57.  
  58.  
  59. FUNCTION tscrMAIN(hTerm: TermHandle; message: Integer;
  60.     p1, p2, p3: LongInt) : LongInt;
  61.  
  62.  
  63. IMPLEMENTATION
  64.  
  65.  
  66.  
  67. FUNCTION TMToolGetConfig(theProcID: Integer; theConfigPtr: Ptr; 
  68.     StrResID: Integer): Ptr;
  69.                 FORWARD;
  70. PROCEDURE GetPhrase(VAR theWorkString: Str255;
  71.     VAR theWord: Str255; VAR theValue: Str255; 
  72.     VAR masterPosition: Integer);
  73.                 FORWARD;
  74. FUNCTION Uppercase(theValue: Str255): Str255;
  75.                 FORWARD;
  76. FUNCTION CheckTheSucker(theValue: Str255; StrResID: Integer): Integer;
  77.                 FORWARD;
  78. FUNCTION GetTheSOBIndex(theWord: Str255; StrResID: Integer): Integer;
  79.                 FORWARD;
  80. FUNCTION TMToolSetConfig(theProcID: Integer; 
  81.     theConfigPtr: Ptr; theStringPtr: Ptr; StrResID: Integer): LongInt; 
  82.                 FORWARD;
  83.  
  84.  
  85.  
  86.  
  87. FUNCTION tscrMAIN(hTerm: TermHandle; message: Integer;
  88.     p1, p2, p3: LongInt) : LongInt;
  89.  
  90. { This function shares the success or failed message with the
  91. toolbox, and also points the toolbox in the right direction
  92. with it's message code (defined in TerminalTool.p) }
  93.  
  94. VAR
  95.     StrResID: Integer;
  96.     tempProcID: Integer;
  97.     tempConfigPtr: Ptr;
  98.  
  99.     tempPrivatePtr: TERMINALPrivatePtr;
  100.     saved: SignedByte;
  101.     
  102.     mBTemp: Str255;
  103.     mBString: Str255;
  104. BEGIN
  105.     tscrMAIN := 0;
  106.     saved := HGetState(Handle(hTerm));
  107.     HLock(Handle(hTerm));
  108.  
  109.     { These are passed to the 'get' and 'set' routines. }
  110.     tempPrivatePtr := TERMINALPrivatePtr(hTerm^^.tmPrivate);
  111.     StrResID := Integer(tempPrivatePtr^.currentLanguage);
  112.     tempProcID := hTerm^^.procID;
  113.     tempConfigPtr := hTerm^^.config;
  114.  
  115.     HSetState(Handle(hTerm), saved);
  116.  
  117.     CASE message OF
  118.         TMMgetMsg:
  119.             tscrMAIN := LongInt(
  120.                 TMToolGetConfig(tempProcID, 
  121.                     tempConfigPtr, StrResID));
  122.         TMMsetMsg:
  123.             tscrMAIN := 
  124.                 TMToolSetConfig(tempProcID, 
  125.                     tempConfigPtr, Ptr(p1), StrResID);
  126.     END; { CASE }
  127. END;
  128.  
  129.  
  130. FUNCTION TMToolGetConfig(theProcID: Integer; theConfigPtr: Ptr; 
  131.     StrResID: Integer): Ptr;
  132.  
  133. { This function returns a pointer to my configuration string, that
  134. gets set up accordingly to current field values from my structure. 
  135. The string is built here using string concats.  It's assumed for
  136. this tool that the delimiter throughout the string is a space. }
  137.  
  138. VAR
  139.     pConfigStr    : Ptr;        
  140.     pConfig        : TERMINALPtr;
  141.     tempStr        : Str255;
  142.     lastPoint    : Integer;
  143.     i            : Integer;
  144.     theStrPtr    : StringPtr;
  145.     storeStr    : Str255;
  146.     tempPtr        : StringPtr;
  147. BEGIN
  148.     pConfig := TerminalPtr(theConfigPtr);
  149.     
  150.     { gets string token and sets it's value according to 
  151.     how the fields are set in my private data structure, 
  152.     adding a space separator between item.
  153.     String when finished:
  154.         WORD value WORD value WORD value            }
  155.  
  156.     GetIndString(tempStr, StrResID, onlineBoolean);
  157.     storeStr := Concat(tempStr, ' ');
  158.     IF pConfig^.onlineBoolean THEN
  159.         GetIndString(tempStr, StrResID, trueID)
  160.     ELSE
  161.         GetIndString(tempStr, StrResID, falseID);
  162.     storeStr := Concat(storeStr, tempStr, ' ');
  163.  
  164.  
  165.  
  166.     GetIndString(tempStr, StrResID, screen132Boolean);
  167.     storeStr := Concat(storeStr, tempStr, ' ');
  168.     IF pConfig^.screenWidth THEN
  169.         GetIndString(tempStr, StrResID, trueID)
  170.     ELSE
  171.         GetIndString(tempStr, StrResID, falseID);
  172.     storeStr := Concat(storeStr, tempStr, ' ');
  173.     
  174.  
  175.  
  176.     GetIndString(tempStr, StrResID, cursorStyleBoolean);
  177.     storeStr := Concat(storeStr, tempStr, ' ');
  178.     IF pConfig^.cursorStyle THEN
  179.         GetIndString(tempStr, StrResID, trueID)
  180.     ELSE
  181.         GetIndString(tempStr, StrResID, falseID);
  182.     storeStr := Concat(storeStr, tempStr);
  183.     
  184.     storeStr := Concat(storeStr, Chr($0));
  185.     
  186.     pConfigStr := NewPtrClear(SIZEOF(storeStr));   { allocate pointer }
  187.  
  188.     { move my Str255 (now zero terminated) to the pointer location.
  189.     The + 1 makes sure that the length byte (Str255) is not included. }
  190.  
  191.     BlockMove(Ptr(Ord4(@storeStr) + 1), pConfigStr, 
  192.         Length(storeStr));
  193.     TMToolGetConfig := pConfigStr;
  194. END;
  195.  
  196.  
  197.  
  198. PROCEDURE GetPhrase(VAR theWorkString: Str255;
  199.     VAR theWord: Str255; VAR theValue: Str255; 
  200.     VAR masterPosition: Integer);
  201.  
  202. { Part of my simple decoder that rips apart the current config
  203. string, returning the first word and it's associated value.  
  204. It also deletes both from the config string since the length
  205. of the string determines whether we are finished. }
  206.  
  207. VAR
  208.     spacePos: Integer;
  209. BEGIN
  210.     spacePos := Pos(' ', theWorkString);
  211.     theWord := Copy(theWorkString, 1, (spacePos - 1));
  212.     Delete(theWorkString, 1, spacePos);
  213.     masterPosition := masterPosition + (Length(theWord) + 1);
  214.     spacePos := Pos(' ', theWorkString);
  215.     IF (spacePos <> 0) THEN
  216.         BEGIN
  217.             theValue := Copy(theWorkString, 1, (spacePos - 1));
  218.             Delete(theWorkString, 1, spacePos);    
  219.         END
  220.     ELSE
  221.         BEGIN
  222.             theValue := Copy(theWorkString, 1, Length(theWorkString));
  223.             Delete(theWorkString, 1, Length(theWorkString));    
  224.         END;
  225.     masterPosition := masterPosition + (Length(theValue) + 1);
  226. END;
  227.  
  228.  
  229.  
  230.  
  231. FUNCTION Uppercase(theValue: Str255): Str255;
  232. { Added to ensure case insensitivity. }
  233. VAR
  234.     stringLength: Integer;
  235.     tempNumber: Integer;
  236.     tempChar: Char;
  237.     i: Integer;
  238. BEGIN
  239.     stringLength := Length(theValue);
  240.     FOR i := 1 to stringLength DO
  241.         BEGIN
  242.             tempNumber := Ord(theValue[i]);
  243.             IF tempNumber > 91 THEN
  244.                 BEGIN
  245.                     tempNumber := tempNumber - 32;
  246.                     theValue[i] := Chr(tempNumber);
  247.                 END;
  248.         END;
  249.     Uppercase := theValue;
  250. END;
  251.  
  252.  
  253. FUNCTION CheckTheSucker(theValue: Str255; StrResID: Integer): Integer;
  254. { Checks the value returned to make sure it's within range.  If 
  255. not, the entire config fails. }
  256. VAR
  257.     trueString: Str255;
  258.     falseString: Str255;
  259.     tempString: Str255;
  260.  
  261.     mBString: Str255;
  262.     mBTemp: Str255;
  263. BEGIN
  264.     CheckTheSucker := 2;
  265.     theValue := Uppercase(theValue);
  266.     GetIndString(tempString, StrResID, trueID);
  267.     trueString := Uppercase(tempString);
  268.     GetIndString(tempString, StrResID, falseID);
  269.     falseString := Uppercase(tempString);
  270.  
  271.     IF theValue = falseString THEN
  272.         CheckTheSucker := 0;
  273.     IF theValue = trueString THEN
  274.         CheckTheSucker := 1;
  275. {    
  276.     NumToString(LongInt(theItem), mBTemp);
  277.     mBString := Concat('The string is: ', mBTemp, 'PROOF');
  278.     DebugStr(mBString); 
  279. }
  280. END;
  281.  
  282.  
  283. FUNCTION GetTheSOBIndex(theWord: Str255; StrResID: Integer): Integer;
  284.  
  285. { This function checks the phrase returned to make sure it matches
  286. one of my resource strings.  If not, the entire config fails. }
  287.  
  288. VAR
  289.     Phrase1String: Str255;
  290.     Phrase2String: Str255;
  291.     Phrase3String: Str255;
  292.     tempString: Str255;
  293. BEGIN
  294.     GetTheSOBIndex := 4;
  295.     theWord := Uppercase(theWord);
  296.     GetIndString(tempString, StrResID, onlineBoolean);
  297.     Phrase1String := Uppercase(tempString);
  298.     GetIndString(tempString, StrResID, screen132Boolean);
  299.     Phrase2String := Uppercase(tempString);
  300.     GetIndString(tempString, StrResID, cursorStyleBoolean);
  301.     Phrase3String := Uppercase(tempString);
  302.  
  303.     IF theWord = Phrase1String THEN
  304.         GetTheSOBIndex := 1;
  305.     IF theWord = Phrase2String THEN
  306.         GetTheSOBIndex := 2;
  307.     IF theWord = Phrase3String THEN
  308.         GetTheSOBIndex := 3;
  309. END;
  310.  
  311.  
  312.  
  313. FUNCTION TMToolSetConfig(theProcID: Integer; 
  314.     theConfigPtr: Ptr; theStringPtr: Ptr; StrResID: Integer): LongInt; 
  315.  
  316. { This main guy rips up the current config string in order to set 
  317. values in this tool's private structure.  Currently it's function
  318. assumes a space delimited string and it does not utilize the script
  319. manager's tokenizing functions.  The variable 'masterPosition' 
  320. is used here to return the approx. character position of any failures
  321. that may be encountered. }
  322.  
  323. VAR
  324.     theCharCount: Integer;
  325.     theWordIndex: Integer;
  326.     theValueIndex: Integer;
  327.     masterPosition: Integer;
  328.     tempString: Str255;
  329.     theWorkString: Str255;
  330.     theWord: Str255;
  331.     theValue: Str255;
  332.     pConfig: TerminalPtr;
  333.     theControlArray: PACKED ARRAY [0..511] of Char;
  334. BEGIN
  335.     pConfig := TerminalPtr(theConfigPtr);
  336.     
  337.     { Ugliness!  Here I assume the CString is less than 512 chars long. 
  338.     I really should exit otherwise. }
  339.     
  340.     BlockMove(theStringPtr, Ptr(Ord4(@theControlArray)), 512);
  341.     theCharCount := Scaneq(512, Chr(0), theControlArray);
  342.     IF theCharCount < 255 THEN
  343.         BlockMove( 
  344.             Ptr(Ord4(@theControlArray)), 
  345.             Ptr(Ord4(@theWorkString) + 1), theCharCount);
  346.     theWorkString[0] := Char(theCharCount);
  347.     
  348.     masterPosition := 0;     { Init the char pos counter }
  349.     
  350.     WHILE Length(theWorkString) > 0 DO
  351.         BEGIN
  352.             GetPhrase(theWorkString, theWord, theValue, 
  353.                 masterPosition);
  354.             theWordIndex := GetTheSOBIndex(theWord, StrResID);
  355.             theValueIndex := CheckTheSucker(theValue, StrResID);
  356.             IF (theValueIndex > 1) OR (theWordIndex > 3) THEN
  357.                 BEGIN
  358.                     TMToolSetConfig := LongInt(masterPosition);
  359.                     Exit(TMToolSetConfig);
  360.                 END;
  361.             CASE theWordIndex OF
  362.                 onlineBoolean:
  363.                     pConfig^.onlineBoolean := Boolean(theValueIndex);
  364.                 screen132Boolean:
  365.                     pConfig^.screenWidth := Boolean(theValueIndex);
  366.                 cursorStyleBoolean:
  367.                     pConfig^.cursorStyle := Boolean(theValueIndex);
  368.                 OTHERWISE
  369.                     BEGIN
  370.                         TMToolSetConfig := 4000;
  371.                         Exit(TMToolSetConfig);
  372.                     END;
  373.             END; { CASE }
  374.         END; { WHILE }
  375.     
  376.     TMToolSetConfig := 0;
  377. END;
  378.  
  379.  
  380.  
  381.  
  382.  
  383. END.